package dntm;

import edu.cn.nlsde.Utils.IOUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.*;

/**
 * Created by dell on 2016/6/16.
 */
public class DNTM {
    public static void format_doc(String path) throws IOException {
        BufferedReader reader = IOUtil.getReader(path);
        String line;
        HashMap<String, HashMap<Integer, Integer>> wordmap = new HashMap<>();
        int docid = 0;
        while ((line = reader.readLine()) != null) {
            String[] wordlist = line.split("\\s+");
            for (String word : wordlist) {
                if (!wordmap.containsKey(word)) {
                    wordmap.put(word, new HashMap<Integer, Integer>());
                }
                Integer nums = wordmap.get(word).get(docid);
                if (nums == null) {
                    wordmap.get(word).put(docid, 1);
                } else {
                    wordmap.get(word).put(docid, nums + 1);
                }
            }
            docid++;
        }
        reader.close();

        BufferedWriter writer = IOUtil.getWriter(path + ".doc");
        reader = IOUtil.getReader(path);
        while ((line = reader.readLine()) != null) {
            String[] wordlist = line.split("\\s+");
            for (String word : wordlist) {
                HashMap<Integer, Integer> selfDoclist = wordmap.get(word);
                for (int selfdocid : selfDoclist.keySet()) {
                    if (selfDoclist.get(selfdocid) > 0) {
                        for (int i = 0; i < selfDoclist.get(selfdocid); i++) {
                            writer.write(selfdocid + " ");
                        }
                    }
                }
            }
            writer.write("\n");
//            break;
        }
        reader.close();
        writer.close();
    }

    public static void word_pair(String path) throws IOException {
        BufferedReader reader = IOUtil.getReader(path);
        String line;
        HashMap<String, Integer> wordmap = new HashMap<>();
        int docid = 0;
        while ((line = reader.readLine()) != null) {
            String[] wordlist = line.split("\\s+");
            for (String word1 : wordlist) {
                for (String word2 : wordlist) {
                    if (word1.equals(word2)) {
                        continue;
                    }
                    String t = word1 + "_" + word2;
                    if (word1.compareTo(word2) > 0) {
                        t = word2 + "_" + word1;
                    }
                    Integer ones = wordmap.get(t);
                    if (ones == null) {
                        wordmap.put(t, 1);
                    } else {
                        wordmap.put(t, ones + 1);
                    }
                }
            }
        }
        BufferedWriter writer = IOUtil.getWriter(path + ".wordpair");
        ArrayList<Map.Entry<String, Integer>> results = new ArrayList<>(wordmap.entrySet());
        Collections.sort(results, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return (o2.getValue().compareTo(o1.getValue()));
            }
        });
        for (Map.Entry<String, Integer> item : results) {
            writer.write(item.getKey() + "\t" + item.getValue() + "\n");
        }
        System.out.println(wordmap.size());
    }

    public static void pdoc(String path) throws IOException {
        BufferedReader reader = IOUtil.getReader(path);
        String line;
        HashMap<String, HashMap<String, Integer>> wordmap = new HashMap<>();
        HashMap<String, Integer> wordmap_count = new HashMap<>();
        int docnum = 0;
        while ((line = reader.readLine()) != null) {
            docnum++;
            String[] wordlist = line.split("\\s+");
            for (String word1 : wordlist) {
                HashMap<String, Integer> storelist = wordmap.get(word1);
                if (storelist == null) {
                    storelist = new HashMap<>();
                    wordmap.put(word1, storelist);
                }

                for (String word2 : wordlist) {
                    Integer the_num = storelist.get(word2);
                    if (the_num == null) {
                        storelist.put(word2, 1);
                    } else {
                        storelist.put(word2, the_num + 1);
                    }
                }
            }
        }
        reader.close();

        BufferedWriter writer = IOUtil.getWriter(path + ".pdoc");
        reader = IOUtil.getReader(path);
        while ((line = reader.readLine()) != null) {
            String[] wordlist = line.split("\\s+");
            for (String word : wordlist) {
                writer.write(word);
                int self_num = wordmap.get(word).get(word);
                HashMap<String, Integer> store_word_list = wordmap.get(word);
                for (String sub_word : store_word_list.keySet()) {
                    int sub_num = wordmap.get(sub_word).get(sub_word);
                    if (store_word_list.get(sub_word) * store_word_list.get(sub_word) / (self_num * sub_num) > 0.1) {
                        writer.write(" " + sub_word);
                    }
                }
                writer.write(" ");

            }
            writer.write("\n");
//            break;
        }
        reader.close();
        writer.close();
    }


    public static void main(String[] args) throws IOException {
//        format_doc("data4/new-tweet/new-tweet.data");
        pdoc("data4/new-tweet/new-tweet.data");
    }
}
